/**********************************************************
 * File
 *	commonpasswords.js
 * Description
 *	Provides functionality for working with the list of
 *	common passwords.
 *********************************************************/

/*
 * Name: getCommonPasswords()
 * Desc: Returns an array of all common passwords from the database.
 */
badpass.getCommonPasswords = function() {
	var conn = badpass.getDatabaseConnection();
	var statement = conn.createStatement("SELECT * FROM common_passwords ORDER BY password ASC");
	var results = new Array();
	while(statement.executeStep()) {
		//Gecko 1.9.0 and below don't support the row property on a statement object
		if(this.isAtLeastGecko191) {
			results.push(statement.row.password);
		} else {
			results.push(statement.getString(0));
		}
	}
	statement.finalize();
	conn.close();
	return results;
}

/*
 * Name: addPassword()
 * Desc: Adds a password to the database.
 */
badpass.addPassword = function(password) {
	var conn = badpass.getDatabaseConnection();
	
	//Gecko 1.9.0 and below do not support the :param method of binding
	//query parameters or the params property
	var statement = null;
	if(this.isAtLeastGecko191) {
		statement = conn.createStatement("INSERT INTO common_passwords VALUES(:password)");
		statement.params.password = password;
	} else {
		statement = conn.createStatement("INSERT INTO common_passwords VALUES(?1)");
		statement.bindStringParameter(0, password);
	}
	
	statement.execute();
	statement.finalize();
	conn.close();
}

/*
 * Name: deletePassword()
 * Desc: Deletes a password from the database.
 */
badpass.deletePassword = function(password) {
	var conn = badpass.getDatabaseConnection();
	
	var statement = null;
	if(this.isAtLeastGecko191) {
		statement = conn.createStatement("DELETE FROM common_passwords WHERE password=:password");
		statement.params.password = password;
	} else {
		statement = conn.createStatement("DELETE FROM common_passwords WHERE password=?1");
		statement.bindStringParameter(0, password);
	}
	
	statement.execute();
	statement.finalize();
	conn.close();
}

/*
 * Name: isCommonPassword()
 * Desc: Returns true if the specified password exists in the database.
 */
badpass.isCommonPassword = function(password) {
	var conn = badpass.getDatabaseConnection();
	
	var statement = null;
	if(this.isAtLeastGecko191) {
		statement = conn.createStatement("SELECT COUNT(*) AS count FROM common_passwords WHERE password=:password");
		statement.params.password = password;
	} else {
		statement = conn.createStatement("SELECT COUNT(*) AS count FROM common_passwords WHERE password=?1");
		statement.bindStringParameter(0, password);
	}
	
	statement.executeStep();
	
	var count = -1;
	if(this.isAtLeastGecko191) {
		count = statement.row.count;
	} else {
		count = statement.getInt32(0);
	}
	
	statement.finalize();
	conn.close();
	if(count == "1") {
		return true;
	} else {
		return false;
	}
}
